#include "ThPSCamera.h"
#include "Object3D\Object3D.h"
#include <assert.h>

#if defined (_DEBUG)
#include "Memory\MemLeaks.h"
#endif

//-------------------------------------------------
CThPSCamera::CThPSCamera(float _fZNear, float _fZFar, float _fFOV, float _fAspectRatio, float _fZoom, CObject3D* _pObject)
	: CCamera(_fZNear, _fZFar, _fFOV, _fAspectRatio, _pObject)
	, m_fZoom(_fZoom)
{

}

//-------------------------------------------------
CThPSCamera::CThPSCamera()
	: CCamera()
	, m_fZoom(1.f)
{

}

//-------------------------------------------------
CThPSCamera::~CThPSCamera()
{

}

//-------------------------------------------------
Vect3f CThPSCamera::GetDirection() const
{
	assert(m_pObject3D);
	return (m_pObject3D->GetPosition() - GetEye());
}

//-------------------------------------------------
Vect3f CThPSCamera::GetEye() const
{
	assert(m_pObject3D);

	float yaw = m_pObject3D->GetYaw();
	float pitch	= m_pObject3D->GetPitch();
	Vect3f pos = m_pObject3D->GetPosition();

	//Pasamos de coordenadas esfericas a coordenadas cartesianas
	Vect3f vEyePt(	m_fZoom * cos(yaw) * cos(pitch), 
					m_fZoom * sin(pitch),
					m_fZoom * sin(yaw) * cos(pitch) );

	return (pos - vEyePt); 
}

//-------------------------------------------------
Vect3f CThPSCamera::GetLookAt() const
{
	assert(m_pObject3D);
	return m_pObject3D->GetPosition();
}

//-------------------------------------------------
Vect3f CThPSCamera::GetVecUp() const
{
	assert(m_pObject3D);

	float yaw	= m_pObject3D->GetYaw();
	float pitch	= m_pObject3D->GetPitch();

	Vect3f vUpVec(	-cos(yaw) * sin(pitch), 
					cos(pitch), 
					-sin(yaw) * sin(pitch) );

	return vUpVec;
}

//-------------------------------------------------
void CThPSCamera::SetZoom(float _fZoom)
{
	m_fZoom = _fZoom;

	if( m_fZoom > m_fZFar*0.8f )
	{
		m_fZoom = m_fZFar*0.8f;
	}
	else if( m_fZoom < m_fZNear*2.f) 
	{
		m_fZoom = m_fZNear*2.f;
	}
}

//-------------------------------------------------
void CThPSCamera::AddZoom(float _fZoom)
{
	m_fZoom += _fZoom;

	if( m_fZoom > m_fZFar*0.8f )
	{
		m_fZoom = m_fZFar*0.8f;
	}
	else if( m_fZoom < m_fZNear*2.f) 
	{
		m_fZoom = m_fZNear*2.f;
	}
}

//-------------------------------------------------
float CThPSCamera::GetZoom() const
{
	return m_fZoom;
}